import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
// import '../../provider/counter.dart';
import '../../provider/cart.dart';
import '../../provider/route_change.dart';
import '../../model/cart.dart';

class ShopCartPage extends StatelessWidget {
  // 获取数据
  Future<String> _getCartsList(context) async {
    // 重新运行的时候，去本地取一下
    await Provide.value<CartProvide>(context).getCartsList();

    return '请求数据了';
  }

  Widget _buildCartItem(BuildContext context, Cart cart) {
    return Container(
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setHeight(200),
      decoration: BoxDecoration(
          color: Colors.white,
          border:
              Border(bottom: BorderSide(width: 1.0, color: Colors.black12))),
      child: Row(
        children: <Widget>[
          Container(
            width: ScreenUtil().setWidth(100),
            height: ScreenUtil().setHeight(200),
            child: Checkbox(
                value: cart.isSelect,
                onChanged: null,
                activeColor: Colors.pink),
          ),
          Container(
            width: ScreenUtil().setWidth(140),
            height: ScreenUtil().setHeight(180),
            margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
            padding: EdgeInsets.all(5.0),
            decoration: BoxDecoration(
                border: Border.all(width: 1.0, color: Colors.black12)),
            child: Image.network(cart.goodsPic),
          ),
          Container(
            width: ScreenUtil().setWidth(350),
            height: ScreenUtil().setHeight(180),
            padding: EdgeInsets.fromLTRB(10, 10, 10, 5),
            alignment: Alignment.topLeft,
            child: Column(
              children: <Widget>[
                Container(
                    width: ScreenUtil().setWidth(350),
                    height: ScreenUtil().setHeight(60),
                    child: Text(
                      '${cart.goodsName}',
                      style: TextStyle(fontSize: ScreenUtil().setSp(24)),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    )),
                Container(
                  width: ScreenUtil().setWidth(350),
                  height: ScreenUtil().setHeight(80),
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 25,
                        height: 25,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            border:
                                Border.all(width: 1.0, color: Colors.black26)),
                        child: InkWell(
                          onTap: () {
                            Provide.value<CartProvide>(context)
                                .changeGoodsCountById(cart.goodsId, 'minus');
                          },
                          child: Text('-'),
                        ),
                      ),
                      Container(
                          width: 25,
                          height: 25,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              border: Border(
                                  top: BorderSide(
                                      width: 1.0, color: Colors.black26),
                                  bottom: BorderSide(
                                      width: 1.0, color: Colors.black26))),
                          child: Text('${cart.count}',
                              style: TextStyle(
                                  color: Colors.black54,
                                  fontSize: ScreenUtil().setSp(28)))),
                      Container(
                          width: 25,
                          height: 25,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              border: Border.all(
                                  width: 1.0, color: Colors.black26)),
                          child: InkWell(
                            onTap: () {
                              Provide.value<CartProvide>(context)
                                  .changeGoodsCountById(cart.goodsId, 'add');
                            },
                            child: Text('+'),
                          ))
                    ],
                  ),
                )
              ],
            ),
          ),
          Container(
            width: ScreenUtil().setWidth(150),
            height: ScreenUtil().setHeight(150),
            margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
            alignment: Alignment.center,
            child: Column(
              children: <Widget>[
                Text(
                  '￥${cart.goodsPrice}',
                  style: TextStyle(fontSize: ScreenUtil().setSp(24)),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
                InkWell(
                  onTap: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return new AlertDialog(
                            title: new Text("提示"),
                            content: new Text("确定删除该商品吗？"),
                            actions: <Widget>[
                              new FlatButton(
                                onPressed: () {
                                  Navigator.of(context).pop();

                                  Provide.value<CartProvide>(context)
                                      .deleteGoodsById(cart.goodsId);
                                },
                                child: new Text("确认"),
                              ),
                              new FlatButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                                child: new Text("取消"),
                              ),
                            ],
                          );
                        });
                  },
                  child: Icon(
                    Icons.delete_forever,
                    color: Colors.black26,
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('购物车')),
        body: FutureBuilder(
          future: _getCartsList(context),
          builder: (context, snapshot) {
            List cartsList = Provide.value<CartProvide>(context).cartsList;
            if (snapshot.hasData && cartsList != null) {
              return Container(child:
                  Provide<CartProvide>(builder: (context, child, cartProvide) {
                if (cartProvide.cartsList.length > 0) {
                  return Stack(children: <Widget>[
                    ListView.builder(
                        itemCount: cartProvide.cartsList.length,
                        itemBuilder: (context, index) {
                          return _buildCartItem(
                              context, cartProvide.cartsList[index]);
                        }),
                    Positioned(
                        bottom: 0,
                        left: 0,
                        width: ScreenUtil().setWidth(750),
                        height: ScreenUtil().setWidth(100),
                        child: Container(
                          decoration: BoxDecoration(color: Colors.white),
                          child: Row(
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(100),
                                height: ScreenUtil().setHeight(100),
                                child: Checkbox(
                                    value: true,
                                    onChanged: null,
                                    activeColor: Colors.white,
                                    checkColor: Colors.pinkAccent),
                              ),
                              Container(
                                  width: ScreenUtil().setWidth(470),
                                  height: ScreenUtil().setHeight(100),
                                  child: Column(
                                    children: <Widget>[
                                      Container(
                                        width: ScreenUtil().setWidth(470),
                                        margin: EdgeInsets.only(right: 8.0),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.end,
                                          children: <Widget>[
                                            Text('合计: ',
                                                style: TextStyle(
                                                    color: Colors.black,
                                                    fontSize: ScreenUtil()
                                                        .setSp(30))),
                                            Text(
                                                '￥${cartProvide.allPrice.toStringAsFixed(2)} ',
                                                style: TextStyle(
                                                    color: Color.fromRGBO(
                                                        234, 81, 65, 1),
                                                    fontSize:
                                                        ScreenUtil().setSp(30)))
                                          ],
                                        ),
                                      ),
                                      Container(
                                          alignment: Alignment.centerRight,
                                          margin: EdgeInsets.only(right: 8.0),
                                          child: Text('满10元免配送费,预约免配送费',
                                              style: TextStyle(
                                                  color: Colors.black54,
                                                  fontSize:
                                                      ScreenUtil().setSp(24))))
                                    ],
                                  )),
                              Container(
                                width: ScreenUtil().setWidth(180),
                                height: ScreenUtil().setHeight(100),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color.fromRGBO(234, 82, 62, 1)),
                                child: Text(
                                  '结算(${cartProvide.allCount})',
                                  style: TextStyle(
                                      fontSize: ScreenUtil().setSp(26),
                                      color: Colors.white),
                                ),
                              )
                            ],
                          ),
                        ))
                  ]);
                } else {
                  return Container(
                    child: Column(
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setWidth(750),
                          height: ScreenUtil().setHeight(400),
                          margin: EdgeInsets.only(bottom: 10.0),
                          alignment: Alignment.bottomCenter,
                          child: Text('还没有购买商品哦~'),
                        ),
                        RaisedButton(
                          onPressed: () {
                            Provide.value<RouteChangeProvide>(context)
                                .changeRoute(0);
                          },
                          child: Text('去购物'),
                        )
                      ],
                    ),
                  );
                }
              }));
            } else {
              return Container(
                width: ScreenUtil().setWidth(750),
                height: ScreenUtil().setHeight(600),
                child: Center(
                  child: Text('数据加载中...'),
                ),
              );
            }
          },
        ));
  }
}
